function getBuildData() {
  var c = le_nk()
    , d = le_ok()
    , e = le_mj;
    le_mj = le_nj.Average;
    le_1h();
  var f = {
    data: le_En(),
    ehp: le_Kn(),
    buffs: le_Gn(),
    tags: le_Jn(c, d)
  };
  le_mj = e;
  le_1h();

  var data = le_Bd();
  var equipSkills = [];
  for (var i = 0; i < data.skillTrees.length; i++) {
    equipSkills.push(data.skillTrees[i].treeID);
  }
  var equipKeys = Object.keys(data.equipment);
  var uniqueKeys = [];
  for (var i = 0; i < equipKeys.length; i++) {
    var equipData = data.equipment[equipKeys[i]];
    if (equipData.id.indexOf('U') === 0) {
      uniqueKeys.push(equipData.id);
    }
  }

  var result = {
    data: data,
    equipSkills: equipSkills,
    uniqueKeys: uniqueKeys,
    tags: [c, d],
    precalcData: f,
    created_for_build: window.gameVersion,
    data_version: window.gameVersion,
    level: le_v.charLevel,
    class: le_u.g,
    mastery: le_u.ja,
  };

  return result;
}

function exportBuildData() {
  var buildData = getBuildData();
  le_work(buildData.data);
}

function setBuildData(data) {
  window.buildInfo = data;
  le_Xh();
  var a = window.buildInfo;
  var b = a.data;
  le_Qh = window.migrateToLatest ? le_ja(window.dataVersion) : a.created_for_build;
  le_Yh(b);
  le_Zh();
}

function importBuildData(data) {
  le_Yh(data);
}

function showFilter() {
  le_Eh.show();
}

window.addEventListener('message', function (e) {
  if (!e.data) return;
  var source = e.data.source;
  var action = e.data.action;
  var payload = e.data.payload;
  if (source !== 'd2core' || (e.origin.indexOf('localhost') === -1 && e.origin.indexOf('d2core.com') === -1)) return;
  if (action === 'getBuildData') {
    var buildData = getBuildData();
    e.source.postMessage({
      source: source,
      action: action,
      payload: {
        data: buildData,
        callback: payload.callback,
      },
    }, '*');
  } else if (action === 'setBuildData') {
    setBuildData(payload);
  } else if (action === 'importBuildData') {
    importBuildData(payload);
  } else if (action === 'exportBuildData') {
    exportBuildData(payload);
  } else if (action === 'showFilter') {
    showFilter(payload);
  }
});